package qdu.com.servlet;

import com.fasterxml.jackson.databind.ObjectMapper;

import org.apache.hdfs.FileUtil;
import qdu.com.bean.WJsonBean2;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@WebServlet("/avg")
public class AvgPerHourByWeatherServlet extends HttpServlet {
    ObjectMapper mapper = new ObjectMapper();
    List<WJsonBean2> list = new ArrayList<WJsonBean2>();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        Map<Integer, String[]> items = FileUtil.ReadPartFile(getServletContext().getRealPath("/output")+"/part-r-00002");
        Map<Integer, String[]> items = FileUtil.ReadPartFile(getServletContext().getRealPath("/output/output")+"/part-r-00002");
        for (Integer key:
                items.keySet()) {
            String[] value = items.get(key); // 3列数据
            list.add(new WJsonBean2(value[0],value[1],value[2]));
        }
        String json = mapper.writeValueAsString(list);
        System.out.println(json);
        resp.setHeader("content-type", "text/json;charset=utf-8");
        resp.getWriter().write(json);
    }

//    //test
//    public static void main(String[] args) throws IOException {
//        ObjectMapper mapper = new ObjectMapper();
//        List<WJsonBean2> list = new ArrayList<WJsonBean2>();
//        Map<Integer, String[]> items = FileUtil.ReadPartFile("output/part-r-00002");
//        for (Integer key:
//             items.keySet()) {
//            String[] value = items.get(key); // 3列数据
//            list.add(new WJsonBean2(value[0],value[1],value[2]));
//        }
////        for (WJsonBean2 w:
////             list) {
////            System.out.println(w.getWeather()+" "+w.getCasual()+" "+" "+w.getRegistered());
////        }
//        String json = mapper.writeValueAsString(list);
//        System.out.println(json);
//    }
}
